#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;

int main()
{
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int ncase,n,m,a,b;
    cin>>ncase;
    while(ncase--)
    {
        int k=0;
        while(cin>>n>>m, n||m)
        {
            k++;
            int cnt=0;
            for(b=2;b<n;b++)
            for(a=1;a<b;a++)
            {
                if((a*a+b*b+m)%(a*b) ==0) cnt++;
            }
            printf("Case %d: %d\n",k,cnt);
        }
        if(ncase>0) cout<<endl;
    }
    return 0;
}
